﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.Business;
using Enterprise.Management.Gridview;
using Enterprise.Management.DataTemplates;
using Enterprise.Management.Caches;
using Enterprise.Management.Data;

namespace Enterprise.Management.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var user = HtmlCache.GetUser();
            if (user.User_RoleID == (int)RoleType.Admin)
            {
                return RedirectToAction("Admin");
            }
            else
            {
                return RedirectToAction("Portal");
            }
        }

        [HttpPost]
        public ActionResult Index(GridviewPage page)
        {
            var gridview = new Gridview.Gridview(page);
            gridview.Header.Add("姓名", "User_Name");
            gridview.Header.Last.SetProperty("style", "width:100px");
            gridview.Header.Add("电子邮箱", "User_Email");
            gridview.Header.Add("角色", "DP_Role.Role_Name");
            gridview.Header.Add("网点", "DP_Category.Category_Name");
            gridview.Header.Add(string.Empty);
            gridview.Header.Last.SetProperty("style", "width:80px");
            gridview.Header.Last.RowDataTemplate = new EditDeleteDataTemplate("_EditDeleteDataTemplatePartial")
            { 
                Name = "User_ID",
                Edit = Url.Action("User", "Edit") + "?uid={0}",
                Delete = "delUser({0})"
            };

            DeptPortalsDataContext context = new DeptPortalsDataContext();
            var query = from u in context.DP_User
                        select u;
            var data = IntelligentQueryBuilder.Create<DP_User>(query, gridview.Page);

            gridview.Fill(data);

            return PartialView("_GridviewPartial", gridview);
        }

        public ActionResult Portal()
        {
            return View();
        }

        public ActionResult Admin()
        {
            return View();
        }

        public ActionResult ExpireReminder(GridviewPage page)
        { 
            var gridview = new Gridview.Gridview(page);
            gridview.Header.Add("营业部名称", "Category_Name");
            gridview.Header.Add("证照类型", "Category_Name");
            gridview.Header.Add("有效期剩余时间（天）");

            return PartialView("_GridviewPartial", gridview);
        }

        public ActionResult BranchUpdates(GridviewPage page)
        {
            var gridview = new Gridview.Gridview(page);
            gridview.Header.Add("营业部名称", "Category_Name");
            gridview.Header.Add("更新类型", "Category_Name");
            gridview.Header.Add("日期");
            gridview.Header.Add("内容");

            return PartialView("_GridviewPartial", gridview);
        }

        public ActionResult TastInfo(GridviewPage page)
        {
            var gridview = new Gridview.Gridview(page);
            gridview.Header.Add("任务描述", "Category_Name");
            gridview.Header.Add("完成情况", "Category_Name");

            return PartialView("_GridviewPartial", gridview);
        }

        #region Category
        [HttpPost]
        public ActionResult GetCategoryBranches(int id)
        {
            CategoryBLL bll = new CategoryBLL();
            var result = from g in bll.GetCategories(id)
                         select new
                         {
                             g.Category_ID,
                             g.Category_Name
                         };
            return Json(result);
        }

        [HttpPost]
        public ActionResult GetCategoryDirectory(int id)
        {
            CategoryBLL bll = new CategoryBLL();
            var child = bll.GetCategory(id);
            var parent = bll.GetParentCategory(id);
            var root = bll.GetParentCategory(parent.Category_ID);
            return Json(new
            {
                root = root.Category_Name,
                parent = parent.Category_Name,
                child = child.Category_Name
            });
        }

        [HttpPost]
        public ActionResult SearchCategories(string name)
        {
            CategoryBLL bll = new CategoryBLL();
            var categories = bll.GetBranchCategories(name);
            return Json(from c in categories
                        select new
                        {
                            c.Category_ID,
                            c.Category_Name
                        });
        }
        #endregion
    }
}
